package com.jia.service.impl;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jia.dto.admin.LoginAdminDto;
import com.jia.entity.Admin;
import com.jia.service.AdminService;
import com.jia.mapper.AdminMapper;
import com.jia.vo.admin.UserInfo;
import com.jia.vo.admin.ValidateCodeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
* @author 贾天旺
* @description 针对表【t_admin】的数据库操作Service实现
* @createDate 2024-04-25 20:51:56
*/
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin>
    implements AdminService{


    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public Map<String, Object> login(LoginAdminDto admin) {
        // 查询
        if(ObjectUtil.isEmpty(admin)){
            System.err.println("======>29行用户为空"+admin);
            return null;
        }

        if (StrUtil.isBlank(admin.getCaptcha())) {
            System.out.println("验证码为空=======>51行");
            return null;
        }
        String vadlicode = redisTemplate.opsForValue().get("validateCode:" + admin.getCodeKey());
        if (StrUtil.isBlank(vadlicode)) {
            System.out.println("redis中验证码为空56行");
            return null;
        }
        if (!vadlicode.equalsIgnoreCase(admin.getCaptcha())) {
            System.out.println("验证码不匹配60行");
            return null;
        }

        //查找用户
        Admin admin1 = this.adminMapper.selectOne(new QueryWrapper<Admin>().eq("username", admin.getUserName()).eq("password", admin.getPassword()));
        if(ObjectUtil.isEmpty(admin1)){
            System.err.println("======>33行用户不存在"+admin1);
            return null;
        }
        //创建token返回
        String replace = UUID.randomUUID().toString().replace("-", "");
        System.out.println("token为===>"+replace);

        Map<String,Object> map=new HashMap<>();
        //存进map中
        map.put("token",replace);
        map.put("refresh_token","");
        //存到redis
        redisTemplate.opsForValue().set("admin:login:"+replace, JSON.toJSONString(admin1),30,TimeUnit.MINUTES);
        return map;
    }

    @Override
    public UserInfo getUserInfo(String token) {
        if(StrUtil.isEmpty(token)){
            System.err.println("======>64行token为空"+token);
            return null;
        }
        //从redis取出token
        String s = redisTemplate.opsForValue().get("admin:login:" + token);
        if(StrUtil.isEmpty(s)){
            System.err.println("70行redis中token===为空"+token);
        }

            //转换用户信息返回
            Admin admin = JSON.parseObject(s, Admin.class);
        UserInfo userInfo =new UserInfo();
            if(ObjectUtil.isNotEmpty(admin)){
                userInfo.setName(admin.getName());
                userInfo.setAvatar(admin.getAvatar());
            }
        //返回
        return userInfo;
    }

    @Override
    public ValidateCodeVo getValidateCode() {
        //定义图形验证码的长、宽、验证码字符数、干扰元素个数
        CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(150, 48, 4, 5);
        //获取验证码的值
        String code = captcha.getCode();
        //用来生成图片的base64字符串
        String imageBase64 = captcha.getImageBase64();
        //把验证码保存到redis 中
        String key = IdUtil.simpleUUID().replace("-", "");
        redisTemplate.opsForValue().set("validateCode:" + key, code, 1, TimeUnit.MINUTES);
        ValidateCodeVo validateCodeVo = new ValidateCodeVo();
        validateCodeVo.setCodeKey(key);
        validateCodeVo.setCodeValue("data:image/png;base64," + imageBase64);
        //响应结果
        return validateCodeVo;
    }

}




